package wellness.activities;

import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import wellness.activities.beans.AgendamentoBean;
import wellness.activities.beans.AtividadeBean;
import wellness.activities.beans.LoadBeans;
import wellness.activities.data.AgendamentoData;
import wellness.activities.data.AtividadeData;
import wellness.activities.data.TempoLivreData;
import wellness.activities.util.Utilidades;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;

public class Main extends Activity {
	/** Called when the activity is first created. */

	private Context context;

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		moveTaskToBack(true);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		context = this;

		Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
				.getDefaultDisplay();
		int width = display.getWidth();
		int widhtMenuItem = (int) Utilidades.convertDpToPixel(100, this);
		double countMenuItemScreen = Math.floor(width / widhtMenuItem);
		int countMenuItens = 5;

		TableLayout table = (TableLayout) findViewById(R.id.tableLayoutContent);
		int margin = (int) Math
				.round((width - (countMenuItemScreen * widhtMenuItem))
						/ (countMenuItemScreen * 2));

		TableRow row = null;
		for (int i = 0; i < countMenuItens; i++) {
			if (i % countMenuItemScreen == 0) {
				row = new TableRow(this);
				table.addView(row, width, widhtMenuItem + (margin * 2));
			}

			ImageView img = new ImageView(this);

			switch (i) {
			case 0:
				img.setImageResource(R.drawable.menu_activity);
				img.setOnClickListener(new View.OnClickListener() {
					public void onClick(View view) {
						redirecionar(Activities.class, view);
					}
				});
				break;
			case 1:
				img.setImageResource(R.drawable.menu_calendar);
				img.setOnClickListener(new View.OnClickListener() {
					public void onClick(View view) {
						redirecionar(Agenda.class, view);
					}
				});
				break;
			case 2:
				img.setImageResource(R.drawable.menu_profile);
				img.setOnClickListener(new View.OnClickListener() {
					public void onClick(View view) {
						redirecionar(Profile.class, view);
					}
				});
				break;
			case 3:
				img.setImageResource(R.drawable.menu_statistics);
				img.setOnClickListener(new View.OnClickListener() {
					public void onClick(View view) {
						redirecionar(Statistics.class, view);
					}
				});
				break;
			case 4:
				img.setImageResource(R.drawable.menu_options);
				img.setOnClickListener(new View.OnClickListener() {
					public void onClick(View view) {
						redirecionar(Options.class, view);
					}
				});
				break;
			default:
			}

			FrameLayout f = new FrameLayout(this);
			f.setPadding(margin, margin, margin, margin);
			f.addView(img, widhtMenuItem, widhtMenuItem);
			row.addView(f, widhtMenuItem + (margin * 2), widhtMenuItem
					+ (margin * 2));
		}

		popularElementos();
	}

	public void redirecionar(Class<?> classe, View view) {
		Intent myIntent = new Intent(view.getContext(), classe);
		startActivityForResult(myIntent, 0);
	}

	public void popularElementos() {
		AtividadeData atividades = new AtividadeData(context);
		atividades.selectAll();
		TempoLivreData temposLivres = new TempoLivreData(context);
		temposLivres.selectAll();
		AgendamentoData agendamentos = new AgendamentoData(context);
		agendamentos.selectAll();
	}

}